local g_platform = nil
local g_arch = nil
local g_mode = nil
local g_verbose = false
local g_execv = nil -- 非 script lifecyle hook scope, 非 on_run 函数无法获取到 os 模块的扩展函数

local g_platform_arch = {
  iphoneos = { "armv7", "armv7s", "arm64", "i386", "x86_64" },
  watchos = { "arm64_32", "armv7k" },
  appletvos = { "arm64" },
  iphonesimulator = { "arm64", "x86_64" },
  watchsimulator = { "arm64", "x86_64" },
  appletvsimulator = { "arm64", "x86_64" },
  linux = { "arm64", "x86_64" },
  macosx = { "arm64", "x86_64" },
  mingw = {  "arm64", "x86_64" },
  android = { "armeabi", "arm64-v8a", "armeabi-v7a", "x86_64", "x86", } -- "mips", "mips64"
}


task("xross")
set_category("plugin")


set_menu({
  usage = "xmake xross [platform] [arch]",
  description = "build cross platform",
  options = {
    {'p', "platform", "v", nil, "target platform.\n\tiphoneos|linux|macosx|mingw|android"},
    {'a', "arch", "v", nil, "target arch.\n\tiphoneos: armv7|armv7s|arm64|i386|x86_64\n\tlinux: arm64|x86_64\n\tmacosx: arm64|x86_64\n\tmingw: arm64|x86_64\n\tandroid: arm64-v8a|armeabi-v7a|x86_64"},
    {'ar', "archive", "k", nil, "xcode archive"},
    {'d', "debug", "k", "debug mode", "debug build mode. (default relase)"},
    {'v', "verbose", "k", nil, "verbose mode."},
  }
})

-- help function
local function indexOf(array, value)
  for i, v in ipairs(array) do
      if v == value then
          return i  
      end
  end
  return nil
end

local function printTable(t, indent)
  indent = indent or 0
  local indentStr = string.rep("  ", indent)
  
  if type(t) ~= "table" then
      print(indentStr .. tostring(t))
      return
  end

  print(indentStr .. "{")
  for k, v in pairs(t) do
      local keyStr = tostring(k)
      if type(v) == "table" then
          print(indentStr .. "  " .. keyStr .. " = ")
          printTable(v, indent + 1)
      else
          local valueStr = tostring(v)
          print(indentStr .. "  " .. keyStr .. " = " .. valueStr)
      end
  end
  print(indentStr .. "}")
end
-- help function -- end

local function xmakeBuild()
  if g_platform and g_arch then
    print("🔥 ".. g_platform .. "🌹 " .. g_arch)
  end
  local config = { "f" }
  if g_platform then
    table.insert(config, "-p")
    table.insert(config, g_platform)
  end
  if g_arch then
    table.insert(config, "-a")
    table.insert(config, g_arch)
  end
  if g_mode then
    table.insert(config, "-m")
    table.insert(config, "debug")
  end
  print("⛰️ xmakeBuild: " .. table.concat(config, ", "))
  -- printTable(os)
  -- printTable(sudo)
  g_execv("xmake", config)

  local package = {"package", "-P", "."}
  if g_verbose then
    table.insert(package, "-v")
  end
  g_execv("xmake", package) 
end

local function buildForPlatformArch()
  local hasTarget = false
  local target_arch = g_platform_arch[g_platform]
  if target_arch then
    if g_arch then
      -- build specific arch
      if indexOf(target_arch, g_arch) then
        xmakeBuild()
        hasTarget = true
      end
    else
      -- build all arch
      for _, arch in ipairs(target_arch) do
        g_arch = arch
        xmakeBuild()
      end
      hasTarget = true
    end
  end
  if not hasTarget then
    p = g_platform or "Unknown platform"
    a = g_arch or "Unknown architecture"
    print("🍄" .. p .. "-".. a .. " not supported!") 
  end
end

on_run(function()
  import("core.base.option")
  g_platform = option.get("platform")
  g_arch = option.get("arch")
  g_mode = option.get("debug")
  g_verbose = option.get("verbose")
  g_execv = os.execv
  if g_platform or g_arch then
    buildForPlatformArch() 
  else
    xmakeBuild()
  end
end)